package com.cloudbroker.bcs.platform.ltb.configure.service;

import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bcs.common.configure.HSBCSZooKeeperConfigurator;
import com.cloudbroker.bcs.common.configure.model.TenantInfo;
import com.cloudbroker.bcs.common.zookeeper.ZooKeeperClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class TenantManager {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TenantManager.class);
    
    private static HSBCSZooKeeperConfigurator zkConfigurator;
    
    /**
     * 获取租户列表
     */
    public static List<TenantInfo> getTenantList() {
        return ConfigUtil.getTenantInfos();
    }
    
    /**
     * 从ZooKeeper中删除租户配置信息
     */
    public static void deleteConfig(String tenantId) {
        if (null != zkConfigurator) {
            ZooKeeperClient zkClient = zkConfigurator.getClient();
            
            String configRootPath = HSBCSZooKeeperConfigurator.getConfigRootPath();
            List<String> categoryList = zkClient.getChildren(configRootPath);
            for (String category : categoryList) {
                String path = ZooKeeperClient.buildFullPath(configRootPath, category, tenantId);
                LOGGER.info("#########删除路径" + path + "######");
                zkClient.delete(path);
            }
        } else {
            LOGGER.info("remove local file not implemented, tenantId=" + tenantId);
        }
    }
    
    public static void setZkConfigurator(HSBCSZooKeeperConfigurator zkConfigurator) {
        TenantManager.zkConfigurator = zkConfigurator;
    }
    
}
